package com.health.viewscontrols.settings;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.BD_DD_DRUG;
import com.health.tools.PinyinUtils;
import com.health.view.spinner.DrugNiceSpinner;
import com.health.viewscontrols.NovaActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.NovaPtActivity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;


/**
 * author : xiaoxin
 * date   : 2020/7/25 15:18
 * desc   :
 * update :
 */
public class AddDrugActivity extends NovaPtActivity {

    private static final String TAG = "AddDrugActivity";
    private BD_DD_DRUG bdDdDrug;
    private LinearLayout ll_finish;
    private TextView tv_save_drug;
    private EditText et_drug_name,et_drug_name_py,et_drug_usage,et_drug_dosage,
            et_drug_res1,et_drug_res2,et_drug_res3;
    private DrugNiceSpinner et_drug_type;

    private FDbManager fdbmanager = null;
    private int drugSize;
    private List<String> drugType = new ArrayList<>();
    private String[] type = {"gxy","tnb","jsb"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_drug);
        fdbmanager = NovaApplication.fdbmanger;
        bdDdDrug = (BD_DD_DRUG)getIntent().getSerializableExtra("bean");
        drugSize = getIntent().getIntExtra("size",0);

        ll_finish        =  findViewById(R.id.ll_finish);
        tv_save_drug     =  findViewById(R.id.tv_save_drug);
        et_drug_name     =  findViewById(R.id.et_drug_name);
        et_drug_name_py  =  findViewById(R.id.et_drug_name_py);
        et_drug_usage    =  findViewById(R.id.et_drug_usage);
        et_drug_dosage   =  findViewById(R.id.et_drug_dosage);
        et_drug_type     =  findViewById(R.id.et_drug_type);
        et_drug_res1     =  findViewById(R.id.et_drug_res1);
        et_drug_res2     =  findViewById(R.id.et_drug_res2);
        et_drug_res3     =  findViewById(R.id.et_drug_res3);

        ll_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_save_drug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkEmpty();
            }
        });

        setDrugData();

        drugType.add("高血压");
        drugType.add("糖尿病");
        drugType.add("精神病");
        et_drug_type.attachDataSource(drugType);
        et_drug_type.setSelectedIndex(0);
    }

    private void setDrugData() {
        if (bdDdDrug != null) {
            et_drug_name.setText(bdDdDrug.getDrugs());
            et_drug_name_py.setText(bdDdDrug.getAbbreviation());
            et_drug_usage.setText(bdDdDrug.getUsage());
            et_drug_dosage.setText(bdDdDrug.getDosage());
            et_drug_type.setText(bdDdDrug.getType());
            et_drug_res1.setText(bdDdDrug.getRes1());
            et_drug_res2.setText(bdDdDrug.getRes2());
            et_drug_res3.setText(bdDdDrug.getRes3());
        }
    }

    @Override
    public void findViews() {

    }

    @Override
    public void setListeners() {

    }

    private void checkEmpty() {
        if (TextUtils.isEmpty(et_drug_name.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品名称",Toast.LENGTH_SHORT).show();
            return;
        }
//        if (TextUtils.isEmpty(et_drug_name_py.getText().toString().trim())) {
//            Toast.makeText(this,"请输入药品名称拼音",Toast.LENGTH_SHORT).show();
//            return;
//        }
        if (TextUtils.isEmpty(et_drug_usage.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品用法",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_drug_dosage.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品用量",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_drug_type.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品类型",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_drug_res1.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品剂量",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_drug_res2.getText().toString().trim())) {
            Toast.makeText(this,"请输入药品剂量单位",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(et_drug_res3.getText().toString().trim())) {
            Toast.makeText(this,"请输入每日几次",Toast.LENGTH_SHORT).show();
            return;
        }

        saveDrug();
    }

    private void saveDrug() {
        if (bdDdDrug == null) {
            bdDdDrug = new BD_DD_DRUG();
            //因为主键id不是自增长 所以要查询总条数+1作为主键
            bdDdDrug.setId(drugSize + 1 +"");
            bdDdDrug.setDrugs(et_drug_name.getText().toString().trim());
            if (!TextUtils.isEmpty(et_drug_name_py.getText().toString().trim())) {
                bdDdDrug.setAbbreviation(et_drug_name_py.getText().toString().trim());
            }else {
                bdDdDrug.setAbbreviation(PinyinUtils.getFirstSpell(
                        et_drug_name.getText().toString().trim().toUpperCase()));
            }
            bdDdDrug.setUsage(et_drug_usage.getText().toString().trim());
            bdDdDrug.setDosage(et_drug_dosage.getText().toString().trim());
            bdDdDrug.setType(type[et_drug_type.getSelectedIndex()]);
            bdDdDrug.setRes1(et_drug_res1.getText().toString().trim());
            bdDdDrug.setRes2(et_drug_res2.getText().toString().trim());
            bdDdDrug.setRes3(et_drug_res3.getText().toString().trim());
            boolean result = fdbmanager.saveInInfoDBResult(bdDdDrug);
            if (result) {
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            Log.e(TAG, "saveDrug: "+result );
        }else {
            bdDdDrug.setDrugs(et_drug_name.getText().toString().trim());
            if (!TextUtils.isEmpty(et_drug_name_py.getText().toString().trim())) {
                bdDdDrug.setAbbreviation(et_drug_name_py.getText().toString().trim());
            }else {
                bdDdDrug.setAbbreviation(PinyinUtils.getFirstSpell(
                        et_drug_name.getText().toString().trim().toUpperCase()));
            }
            bdDdDrug.setUsage(et_drug_usage.getText().toString().trim());
            bdDdDrug.setDosage(et_drug_dosage.getText().toString().trim());
            bdDdDrug.setType(type[et_drug_type.getSelectedIndex()]);
            bdDdDrug.setRes1(et_drug_res1.getText().toString().trim());
            bdDdDrug.setRes2(et_drug_res2.getText().toString().trim());
            bdDdDrug.setRes3(et_drug_res3.getText().toString().trim());
            fdbmanager.updateInInfoDBResult(bdDdDrug);
            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}
